<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力中小企业发展 
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德代驾系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
+----------------------------------------------------------------------
| 紧急联系人 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<view class="container" :style="{'--theme-color': themeColor}" v-if="loadEnd">
		<!-- 紧急联系人列表 -->
		<view class="container-main">
			<view class="main-list" v-if="contactsList.length">
				<view class="list-item flex align-items-center" v-for="item in contactsList" :key="item.id" @click="handleEdit(item.id)">
					<view class="item-icon" :style="{'background-image': 'url('+ avatarIcon +')'}" v-if="avatarIcon"></view>
					<view class="item-text flex-item">
						<text class="text text-ellipsis" v-if="item.name">{{item.name}}</text>
						<text class="text">{{item.mobile}}</text>
					</view>
					<view class="item-delete" @click.stop="handleDelete(item.id)">删除</view>
					<view class="item-mask flex align-items-center" v-if="contactLoading">
						<view class="mask-left"></view>
						<view class="mask-center flex-item"></view>
						<view class="mask-right"></view>
					</view>
				</view>
			</view>
			<view class="main-empty" v-else>
				<image class="icon" src="/static/passenger/contacts_empty.png" mode="aspectFit"></image>
				<view class="text">未设置紧急联系人</view>
			</view>
			<view class="main-btn flex align-items-center" @click="handleAdd()">
				<image class="btn-icon" src="/static/passenger/add.png" mode="aspectFit"></image>
				<view class="btn-text flex-item">添加紧急联系人</view>
				<image class="btn-right" src="/static/arrow_right.png" mode="aspectFit"></image>
			</view>
			<view class="main-tips">为了您的行程安全，请设置紧急联系人，联系人将用于行程自动分享和紧急求助告警</view>
		</view>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	import svgData from "@/common/svg.js"
	export default {
		data() {
			return {
				// 页面是否阻止滚动
				pageShow: false,
				// 加载完成
				loadEnd: false,
				// 紧急联系人列表
				contactsList: [],
				// 紧急联系人列表是否加载
				contactLoading: false,
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
				avatarIcon: state => {
					return svgData.svgToUrl("avatar", state.app.themeColor)
				}
			})
		},
		onLoad() {
			uni.showLoading({
				title: "加载中"
			})
			this.getContacts(() => {
				uni.hideLoading()
				this.loadEnd = true
			})
		},
		onShow() {
			if (this.loadEnd) {
				this.contactLoading = true
				this.getContacts(() => {
					this.contactLoading = false
				})
			}
		},
		onPullDownRefresh() {
			this.contactLoading = true
			this.getContacts(() => {
				this.contactLoading = false
				uni.stopPullDownRefresh()
			})
		},
		methods: {
			// 获取紧急联系人列表
			getContacts(fn) {
				this.$util.request("passenger.contacts.list").then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.contactsList = res.data
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取紧急联系人列表 ', error)
				})
			},
			// 添加紧急联系人
			handleAdd() {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/contacts/edit",
					isToken: true,
				})
			},
			// 编辑紧急联系人
			handleEdit(id) {
				this.$util.toPage({
					mode: 1,
					path: "/pages/passenger/mine/contacts/edit?id=" + id,
					isToken: true,
				})
			},
			// 删除紧急联系人
			handleDelete(id) {
				uni.showModal({
					content: " 确认删除此紧急联系人？ \n 点击【确认删除】按钮删除 ",
					confirmText: "确认删除",
					cancelText: "我再想想",
					confirmColor: "#DE2826",
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: "加载中",
								mask: true
							})
							this.$util.request("passenger.contacts.delete", { id }).then(res => {
								uni.hideLoading()
								if (res.code == 1) {
									uni.showToast({
										icon: "success",
										title: "删除成功"
									})
									this.contactLoading = true
									this.getContacts(() => {
										this.contactLoading = false
									})
								} else {
									uni.showToast({
										title: res.msg,
										icon: 'none'
									})
								}
							}).catch(error => {
								uni.hideLoading()
								console.error('删除紧急联系人 ', error)
							})
						}
					}
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 32rpx;

		.container-main {
			.main-list {
				.list-item {
					position: relative;
					border-radius: 32rpx;
					overflow: hidden;
					background: #FFF;
					margin-top: 32rpx;
					padding: 32rpx 48rpx;

					&:first-child {
						margin-top: 0;
					}

					.item-icon {
						width: 64rpx;
						height: 64rpx;
						background-size: 64rpx;
						border-radius: 50%;
					}

					.item-text {
						display: flex;
						justify-content: flex-end;
						align-items: center;

						.text {
							margin-left: 32rpx;
							color: #242629;
							font-size: 28rpx;
							font-weight: 600;
							line-height: 40rpx;
						}
					}

					.item-delete {
						color: #DE2828;
						font-size: 28rpx;
						font-weight: 600;
						line-height: 40rpx;
						margin-left: 24rpx;
					}

					.item-mask {
						position: absolute;
						top: 0;
						left: 0;
						right: 0;
						bottom: 0;
						z-index: 9;
						background: #FFF;
						padding: 32rpx 48rpx;

						.mask-left {
							width: 64rpx;
							height: 64rpx;
							background: #eee;
						}

						.mask-center {
							margin-left: 32rpx;
							height: 40rpx;
							background: #eee;
						}

						.mask-right {
							width: 60rpx;
							height: 40rpx;
							margin-left: 24rpx;
							background: #eee;
						}
					}
				}
			}

			.main-empty {
				display: flex;
				flex-direction: column;
				align-items: center;
				padding: 144rpx 0 32rpx;

				.icon {
					width: 360rpx;
					height: 400rpx;
				}

				.text {
					margin-top: 32rpx;
					color: #8D929C;
					text-align: center;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
				}
			}

			.main-btn {
				border-radius: 32rpx;
				background: #FFF;
				margin-top: 32rpx;
				padding: 44rpx 48rpx;

				.btn-icon {
					width: 40rpx;
					height: 40rpx;
				}

				.btn-text {
					margin-left: 24rpx;
					color: #242629;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
				}

				.btn-right {
					width: 24rpx;
					height: 24rpx;
					margin-left: 24rpx;
				}
			}

			.main-tips {
				color: #8D929C;
				font-size: 24rpx;
				font-weight: 600;
				line-height: 34rpx;
				margin-top: 48rpx;
			}
		}
	}
</style>